package com.nidu.demo.web.system;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.log.api.SysApiAccessLogService;
import com.nidu.demo.log.dto.ApiAccessLogCO;
import com.nidu.demo.log.dto.ApiAccessLogPageQry;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * API访问日志管理
 *
 * @author auto
 */
@Tag(name = "管理后台 - API访问日志")
@RestController
@RequestMapping("/system/api-access-log")
@Validated
@RequiredArgsConstructor
public class SysApiAccessLogController {

    private final SysApiAccessLogService apiAccessLogService;

    @Operation(summary = "获取API访问日志详情", description = "根据 ID 获取API访问日志详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:api-access-log:query')")
    public SingleResponse<ApiAccessLogCO> getApiAccessLogById(@PathVariable Long id) {
        return apiAccessLogService.getById(id);
    }

    @Operation(summary = "分页查询API访问日志", description = "分页获取API访问日志信息")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:api-access-log:query')")
    public PageResponse<ApiAccessLogCO> pageApiAccessLogs(ApiAccessLogPageQry qry) {
        return apiAccessLogService.page(qry);
    }

    @Operation(summary = "删除API访问日志", description = "根据 ID 删除API访问日志")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:api-access-log:delete')")
    public Response deleteApiAccessLog(@PathVariable Long id) {
        return apiAccessLogService.delete(id);
    }

    @Operation(summary = "批量删除API访问日志", description = "根据 ID 列表删除多个API访问日志")
    @PostMapping("/batch-delete")
    @PreAuthorize("@ss.hasPermission('system:api-access-log:delete')")
    public Response deleteApiAccessLogs(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return apiAccessLogService.delete(new HashSet<>(ids));
    }

    @Operation(summary = "清空API访问日志", description = "清空所有API访问日志")
    @PostMapping("/clear")
    @PreAuthorize("@ss.hasPermission('system:api-access-log:delete')")
    public Response clearApiAccessLogs() {
        return apiAccessLogService.clear();
    }

    @Operation(summary = "导出API访问日志", description = "导出API访问日志到Excel")
    @PostMapping("/export")
    @PreAuthorize("@ss.hasPermission('system:api-access-log:export')")
    public Response exportApiAccessLogs(@RequestBody ApiAccessLogPageQry qry) {
        return apiAccessLogService.export(qry);
    }

    @Operation(summary = "获取API访问统计", description = "获取API访问统计信息")
    @GetMapping("/statistics")
    @PreAuthorize("@ss.hasPermission('system:api-access-log:query')")
    public Response getApiAccessStatistics() {
        return apiAccessLogService.getStatistics();
    }
}

